package com.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.project.bean.*;
import com.project.dto.UserOrderEntity;
import com.project.dto.UserOrderInfoProductEntity;
import com.project.service.IBuyCarService;
import com.project.service.IOrderService;
import com.project.service.IProductService;
import com.project.util.SnowIdUtils;
import com.project.vo.GouWuChe;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("order")
public class OrderController {
      @Autowired
      RabbitTemplate rabbitTemplate;
      @Autowired
      private IOrderService service;
      @Autowired
      private IBuyCarService carService;
      @Autowired
      private IProductService productService;
      @RequestMapping("add")
      public String add(String address,double totalPrice,String modeOfPayment,int num, String productNum,double sellPrice,HttpServletRequest request){
           OrderBean orderBean=new OrderBean();
           orderBean.setOrderNum(SnowIdUtils.uniqueLongHex());
           orderBean.setAddress(address);
           orderBean.setTotalPrice(totalPrice);
           orderBean.setModeOfPayment(modeOfPayment);
           UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
           orderBean.setUserNum(userBean.getUserNum());
           List<ProductOrder> list=new ArrayList<>();
           //查找商品
           ProductOrder productOrder=new ProductOrder();
           productOrder.setProductNum(productNum);
           productOrder.setNum(num);
           productOrder.setSellPrice(sellPrice);
           productOrder.setOrderNum(orderBean.getOrderNum());
           ProductBean productBean=productService.findProductInfo(productNum);
           productOrder.setProductName(productBean.getProductName());
           productOrder.setProductType(productBean.getProductType());
           productOrder.setSize(productBean.getSize());
           productOrder.setColor(productBean.getColor());
           list.add(productOrder);
           orderBean.setProductOrders(list);
           service.add(orderBean);
           //需要将订单发送给审核
            //把对象转化成json对象
            UserOrderEntity userOrderEntity=new UserOrderEntity();
            userOrderEntity.setOrderRef(orderBean.getOrderNum());
            userOrderEntity.setUserRef(orderBean.getUserNum());
            userOrderEntity.setTotalPrice(totalPrice);
            userOrderEntity.setStatus("0");
            userOrderEntity.setAddress(address);
            int a=0;
            if(modeOfPayment.equals("支付宝支付")){
                  a=1;
            }
            userOrderEntity.setModeOfPayment(a);
            List<UserOrderInfoProductEntity> list1=new ArrayList<>();
            orderBean.getProductOrders().forEach(n->{
                  UserOrderInfoProductEntity userOrderInfoProductEntity=new UserOrderInfoProductEntity();
                  userOrderInfoProductEntity.setOrderRef(n.getOrderNum());
                  userOrderInfoProductEntity.setProductRef(n.getProductNum());
                  userOrderInfoProductEntity.setProdcutName(n.getProductName());
                  userOrderInfoProductEntity.setProductNumber(n.getNum());
                  userOrderInfoProductEntity.setProductPrice(n.getSellPrice());
                  userOrderInfoProductEntity.setColor(n.getColor());
                  userOrderInfoProductEntity.setType(n.getProductType());
                  list1.add(userOrderInfoProductEntity);
            });
            userOrderEntity.setUserOrderInfoProductEntityList(list1);
            String jsonString = JSONObject.toJSONString(userOrderEntity);
            rabbitTemplate.convertAndSend("webOne","addOrder",jsonString);
          rabbitTemplate.convertAndSend("webOne","addOrderEnd",jsonString);

           return orderBean.getOrderNum();
      }
      @RequestMapping("gouWcPay")
      public GouWuChe gouWcPay(String address,double totalPrice,String modeOfPayment,HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            List<BuyCarBean> list=carService.findUserBuyCar(userBean.getUserNum());
            //创建订单
            OrderBean orderBean=new OrderBean();
            orderBean.setOrderNum(SnowIdUtils.uniqueLongHex());
            orderBean.setAddress(address);
            orderBean.setTotalPrice(totalPrice);
            orderBean.setModeOfPayment(modeOfPayment);
            orderBean.setUserNum(userBean.getUserNum());
            //购物车工具类
            GouWuChe gouWuChe=new GouWuChe();
            //商品明细集合
            List<ProductOrder> list2=new ArrayList<>();
            //获取商品，判断是否有失效的商品
            list.forEach(n->{
                  ProductBean productBean=productService.findByNameAndColorAndSize(n.getProductName(),n.getColor(),n.getSize());
                  if(productBean.getPutStatus()!=0){
                        gouWuChe.setErr(gouWuChe.getErr()+","+productBean.getProductName());
                        gouWuChe.setStatus(1);
                  }else {
                        ProductBean productBean2=productService.findProductInfo(n.getProductNum());
                        ProductOrder productOrder=new ProductOrder();
                        productOrder.setProductName(productBean2.getProductName());
                        productOrder.setProductType(productBean2.getProductType());
                        productOrder.setSize(productBean2.getSize());
                        productOrder.setColor(productBean2.getColor());
                        productOrder.setProductNum(n.getProductNum());
                        productOrder.setNum(n.getNum());
                        productOrder.setSellPrice(n.getProductPrice());
                        productOrder.setOrderNum(orderBean.getOrderNum());
                        list2.add(productOrder);
                  }
            });

            orderBean.setProductOrders(list2);
            if(gouWuChe.status==0){
                  service.add(orderBean);
                  //清空购物车
                  carService.del(userBean.getUserNum());
                  gouWuChe.setOrderNum(orderBean.getOrderNum());
                  //将订单发送给审核
                  //需要将订单发送给审核
                  //把对象转化成json对象
                  UserOrderEntity userOrderEntity=new UserOrderEntity();
                  userOrderEntity.setOrderRef(orderBean.getOrderNum());
                  userOrderEntity.setUserRef(orderBean.getUserNum());
                  userOrderEntity.setTotalPrice(totalPrice);
                  userOrderEntity.setStatus("0");
                  userOrderEntity.setAddress(address);
                  int a=0;
                  if(modeOfPayment.equals("支付宝支付")){
                        a=1;
                  }
                  userOrderEntity.setModeOfPayment(a);
                  List<UserOrderInfoProductEntity> list1=new ArrayList<>();
                  orderBean.getProductOrders().forEach(n->{
                        UserOrderInfoProductEntity userOrderInfoProductEntity=new UserOrderInfoProductEntity();
                        userOrderInfoProductEntity.setOrderRef(n.getOrderNum());
                        userOrderInfoProductEntity.setProductRef(n.getProductNum());
                        userOrderInfoProductEntity.setProdcutName(n.getProductName());
                        userOrderInfoProductEntity.setProductNumber(n.getNum());
                        userOrderInfoProductEntity.setProductPrice(n.getSellPrice());
                        userOrderInfoProductEntity.setColor(n.getColor());
                        userOrderInfoProductEntity.setType(n.getProductType());
                        list1.add(userOrderInfoProductEntity);
                  });
                  userOrderEntity.setUserOrderInfoProductEntityList(list1);
                  String jsonString = JSONObject.toJSONString(userOrderEntity);
                  rabbitTemplate.convertAndSend("webOne","addOrder",jsonString);
                  rabbitTemplate.convertAndSend("webOne","addOrderEnd",jsonString);
                System.out.println("********************************************");
                System.out.println(jsonString);
            }
            return gouWuChe;
      }
      @RequestMapping("findByDelStatus")
      public List<OrderBean> findByDelStatus(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByDelStatus(userBean.getUserNum());
      }
      @RequestMapping("findByDelStatus2")
      public List<OrderBean> findByDelStatus2(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByDelStatus2(userBean.getUserNum());
      }
      @RequestMapping("findByDelStatus3")
      public List<OrderBean> findByDelStatus3(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByDelStatus3(userBean.getUserNum());
      }
      @RequestMapping("findByDelStatus4")
      public List<OrderBean> findByDelStatus4(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByDelStatus4(userBean.getUserNum());
      }
      @RequestMapping("findByDelStatus5")
      public List<OrderBean> findByDelStatus5(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByDelStatus5(userBean.getUserNum());
      }
      @RequestMapping("findByDelStatus6")
      public List<OrderBean> findByDelStatus6(HttpServletRequest request){
            UserBean userBean= (UserBean) request.getSession().getAttribute("loginUser");
            return service.findByDelStatus6(userBean.getUserNum());
      }
      @RequestMapping("findByOrderNum")
      public OrderBean findByOrderNum(String orderNum){
            return service.findByOrderNum(orderNum);
      }
      @RequestMapping("del")
      public String del(String orderNum){
            Boolean fig=service.del(orderNum);
            if(fig){
                  return "ok";
            }
            return "no";
      }
}
